package nlp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;


import opennlp.tools.cmdline.postag.POSModelLoader;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSSample;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.sentdetect.SentenceDetector;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;
import opennlp.tools.util.InvalidFormatException;
import utils.Pair;

public class PortuguesePOSTagger {

	POSModel model = null;
	TokenizerModel tModel = null;
	SentenceModel sModel = null;	
	POSTaggerME tagger = null;
	TokenizerME tokenizer = null;
	public SentenceDetector sentence = null;
	String FILES = "/home/dsbatista/POS-tagger/";

	public PortuguesePOSTagger() throws InvalidFormatException, FileNotFoundException, IOException{
		this.model = new POSModelLoader().load(new File(FILES + "pt.postaggerVerbPP.model"));
		this.tModel = new TokenizerModel(new FileInputStream(FILES + "pt.tokenizerVerbPP.model")); 
		this.sModel = new SentenceModel(new FileInputStream(FILES + "pt.sentDetectVerbPP.model"));
		this.tagger = new POSTaggerME(model); 
		this.tokenizer = new TokenizerME(tModel);
		this.sentence = new SentenceDetectorME(sModel);
	}

	public Pair<String[],String[]> tagSentence(String sentence){				
		String[] tokens = tokenizer.tokenize(sentence.trim());
		String[] pos_tags = tagger.tag(tokens);			
		Pair<String[], String[]> p = new Pair<String[], String[]>(tokens, pos_tags);
		return p;		
	}
	
	public List<String> tagText(String text) {				
		List<String> tagged_sentences = new LinkedList<String>();				
		//split the text into sentences
		for (String s : sentence.sentDetect(text)) {
			
			//tokenize the sentence and attribute a pos-tag 
			String tokens[] = null;
			tokens = tokenizer.tokenize(s.trim());
			String[] postTags = tagger.tag(tokens);
			
			//build string: token1_postag1 token2_postag2 .... tokenN_postagN
			POSSample tokensTagged = new POSSample(tokens, postTags);			
			String sentenceTagged = tokensTagged.toString();
			String t = sentenceTagged.replaceAll("\\_","\\/");			
			tagged_sentences.add(t);
		}
		
		return tagged_sentences;
	}
}




